#pragma once

namespace wyt
{
	template<size_t N>
	class bitset
	{
	public:
		bitset()
		{
			//_bits.resize(N/8+1, 0);
			_bits.resize((N >> 3) + 1, 0);
		}

		void set(size_t x)
		{
			//size_t i = x / 8;
			size_t i = x >> 3;
			size_t j = x % 8;

			_bits[i] |= (1 << j);
		}

		void reset(size_t x)
		{
			size_t i = x >> 3;
			size_t j = x % 8;

			_bits[i] &= (~(1 << j));
		}

		bool test(size_t x)
		{
			size_t i = x >> 3;
			size_t j = x % 8;

			return _bits[i] & (1 << j);
		}

	private:
		vector<char> _bits;
	};

	void test_bitset()
	{
		//bitset<100> bs1;
		//bitset<-1> bs2;
		bitset<0xffffffff> bs2;

		bs2.set(10);
		bs2.set(10000);
		bs2.set(8888);

		cout << bs2.test(10) << endl;
		cout << bs2.test(10000) << endl;
		cout << bs2.test(8888) << endl;
		cout << bs2.test(8887) << endl;
		cout << bs2.test(9999) << endl << endl;

		bs2.reset(8888);
		bs2.set(8887);

		cout << bs2.test(10) << endl;
		cout << bs2.test(10000) << endl;
		cout << bs2.test(8888) << endl;
		cout << bs2.test(8887) << endl;
		cout << bs2.test(9999) << endl;
	}

	template<size_t N>
	class twobitset
	{
	public:
		void set(size_t x)
		{
			if (!_bs1.test(x) && !_bs2.test(x)) // 00
			{
				_bs2.set(x); // 01
			}
			else if (!_bs1.test(x) && _bs2.test(x)) // 01
			{
				_bs1.set(x);
				_bs2.reset(x); // 10
			}

			// 10 
		}

		void PirntOnce()
		{
			for (size_t i = 0; i < N; ++i)
			{
				if (!_bs1.test(i) && _bs2.test(i))
				{
					cout << i << endl;
				}
			}
			cout << endl;
		}

	private:
		bitset<N> _bs1;
		bitset<N> _bs2;
	};

	void test_twobitset()
	{
		twobitset<100> tbs;
		int a[] = { 3, 5, 6, 7, 8, 9, 33, 55, 67, 3, 3, 3, 5, 9, 33 };
		for (auto e : a)
		{
			tbs.set(e);
		}

		tbs.PirntOnce();
	}
}